import Vue from "vue";
import Vuex from "vuex";
import vuexPersist from "vuex-persist";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "",
    shop: [],
  },
  mutations: {
    //存储token
    settoken(state, val) {
      state.token = val;
    },
    // 退出登录
    exit(state) {
      state.token = "";
    },
    addshop(state, i) {
      let flag = false;
      state.shop.forEach((item) => {
        if (item.id == i.id) {
          flag = true;
          item.num++;
        }
      });
      if (flag == false) {
        Vue.set(i, "num", 1);
        state.shop.push(i);
      }
    },
  },
  actions: {},
  modules: {},
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
